package com.bytedance.retrofit2;

import com.bytedance.retrofit2.mime.TypedOutput;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

abstract class ParameterHandler<T> {
    ParameterHandler() {
    }

    final ParameterHandler<Iterable<T>> iterable() {
        return new ParameterHandler<Iterable<T>>() {

            @Override
            void apply(RequestBuilder builder, Iterable<T> values) throws IOException {
                if (values != null) {
                    for (T value : values) {
                        ParameterHandler.this.apply(builder, value);
                    }
                }
            }
        };
    }

    abstract void apply(RequestBuilder builder, T value) throws IOException;

    final ParameterHandler<Object> array() {
        return new ParameterHandler<Object>() {
            @Override
            final void apply(RequestBuilder builder, Object values) throws IOException {
                if (values != null) {
                    int v0 = 0;
                    int v1 = Array.getLength(values);
                    while (v0 < v1) {
                        ParameterHandler.this.apply(builder, (T) Array.get(values, v0));
                        ++v0;
                    }
                }
            }
        };
    }

    static final class AddCommonParam<T> extends ParameterHandler<T> {
        private final Converter<T, String> a;

        AddCommonParam(Converter arg2) {
            this.a = Utils.checkNotNull(arg2, "converter == null");
        }

        final void apply(RequestBuilder builder, T value) throws IOException {
            if (value != null) {
                try {
                    builder.n = Boolean.parseBoolean(this.a.convert(value));
                } catch (IOException v0) {
                    throw new RuntimeException("Unable to convert " + value + " to AddCommonParam", ((Throwable) v0));
                }
            }
        }
    }

    static final class ExtraInfo extends ParameterHandler {
        private final Converter a;

        ExtraInfo(Converter arg2) {
            this.a = Utils.checkNotNull(arg2, "converter == null");
        }

        final void apply(RequestBuilder builder, Object value) throws IOException {
            if (value != null) {
                try {
                    builder.o = this.a.convert(value);
                } catch (IOException v0) {
                    throw new RuntimeException("Unable to convert " + value + " to ExtraInfo", v0);
                }
            }
        }
    }

    static final class FieldMap<T> extends ParameterHandler<Map<String, T>> {
        private final Converter<T, String> valueConverter;
        private final boolean encoded;

        FieldMap(Converter<T, String> arg1, boolean arg2) {
            this.valueConverter = arg1;
            this.encoded = arg2;
        }

        @Override
        void apply(RequestBuilder builder, Map<String, T> arg6) throws IOException {
            if (arg6 == null) {
                throw new IllegalArgumentException("Field map was null.");
            }

            Iterator v2 = ((Map) arg6).entrySet().iterator();
            while (v2.hasNext()) {
                Map.Entry<String, T> v0 = (Map.Entry<String, T>) v2.next();
                Object v1 = ((Map.Entry) v0).getKey();
                if (v1 == null) {
                    throw new IllegalArgumentException("Field map contained null key.");
                }

                T value = v0.getValue();
                if (v0 == null) {
                    throw new IllegalArgumentException("Field map contained null value for key \'" + v1 + "\'.");
                }
                builder.b(((String) v1), this.valueConverter.convert(value), this.encoded);
            }
        }
    }

    static final class Path<T> extends ParameterHandler<T> {
        private final String name;
        private final Converter<T, String> valueConverter;
        private final boolean encoded;

        Path(String arg2, Converter arg3, boolean arg4) {
            this.name = Utils.checkNotNull(arg2, "name == null");
            this.valueConverter = arg3;
            this.encoded = arg4;
        }

        final void apply(RequestBuilder builder, T value) throws IOException {
            if (value == null) {
                throw new IllegalArgumentException("Path parameter \"" + this.name + "\" value must not be null.");
            }

            String v2 = this.name;
            Object v0 = this.valueConverter.convert(value);
            boolean v1 = this.encoded;
            if (builder.relativeUrl == null) {
                throw new AssertionError();
            }

            if (v2 == null) {
                throw new IllegalArgumentException("Path replacement name must not be null.");
            }

            if (v0 == null) {
                throw new IllegalArgumentException("Path replacement \"" + v2 + "\" value must not be null.");
            }

            if (v1) {
                try {
                    builder.relativeUrl = builder.relativeUrl.replace("{" + v2 + "}", URLEncoder.encode(String.valueOf(v0), "UTF-8").replace("+", "%20"));
                    return;
                } catch (UnsupportedEncodingException v1_1) {
                    throw new RuntimeException("Unable to convert path parameter \"" + v2 + "\" value to UTF-8:" + (((String) v0)), ((Throwable) v1_1));
                }
            } else {
                builder.relativeUrl = builder.relativeUrl.replace("{" + v2 + "}", String.valueOf(v0));
                return;
            }
        }
    }

    static final class AndroidUriUrl extends ParameterHandler {
        AndroidUriUrl() {

        }

        final void apply(RequestBuilder builder, Object value) {
            if (value == null) {
                throw new NullPointerException("@Url parameter is null.");
            }
            builder.relativeUrl = value.toString();
        }
    }

    static final class Body<T> extends ParameterHandler<T> {
        private final boolean notNull;
        private final Converter<T, TypedOutput> converter;

        Body(boolean notNull, Converter<T, TypedOutput> arg2) {
            this.notNull = notNull;
            this.converter = arg2;
        }

        final void apply(RequestBuilder builder, T value) {
            if (value != null) {
                try {
                    builder.body = this.converter.convert(value);
                } catch (IOException e) {
                    throw new RuntimeException("Unable to convert " + value + " to TypedOutput", e);
                }
            } else if (!this.notNull) {
                throw new IllegalArgumentException("Body parameter value must not be null.");
            }
        }
    }

    static final class Field<T> extends ParameterHandler<T> {
        private final String a;
        private final Converter<T, String> b;
        private final boolean c;

        Field(String arg2, Converter arg3, boolean arg4) {

            this.a = Utils.checkNotNull(arg2, "name == null");
            this.b = arg3;
            this.c = arg4;
        }

        final void apply(RequestBuilder builder, T value) throws IOException {
            if (value != null) {
                builder.b(this.a, this.b.convert(value), this.c);
            }
        }
    }

    static final class Header<T> extends ParameterHandler<T> {
        private final String a;
        private final Converter<T, String> b;

        Header(String arg2, Converter arg3) {

            this.a = Utils.checkNotNull(arg2, "name == null");
            this.b = arg3;
        }

        final void apply(RequestBuilder builder, T value) throws IOException {
            if (value != null) {
                builder.addHeader(this.a, this.b.convert(value));
            }
        }
    }

    static final class HeaderList extends ParameterHandler {
        private final Converter a;

        HeaderList(Converter arg1) {

            this.a = arg1;
        }

        final void apply(RequestBuilder builder, Object value) throws IOException {
            if (value != null) {
                Iterator v1 = ((List) value).iterator();
                while (v1.hasNext()) {
                    Object v0 = this.a.convert(v1.next());
                    builder.addHeader(((com.bytedance.retrofit2.client.Header) v0).name, ((com.bytedance.retrofit2.client.Header) v0).value);
                }
            }
        }
    }

    static final class MaxLength<T> extends ParameterHandler<T> {
        private final Converter<T, String> a;

        MaxLength(Converter arg2) {
            this.a = Utils.checkNotNull(arg2, "converter == null");
        }

        final void apply(RequestBuilder builder, T value) throws IOException {
            if (value != null) {
                try {
                    builder.m = Integer.parseInt(this.a.convert(value));
                } catch (IOException v0) {
                    throw new RuntimeException("Unable to convert " + value + " to MaxLength", v0);
                }
            }
        }
    }

    static final class Method extends ParameterHandler {
        private final String a;
        private final Converter b;

        Method(String arg2, Converter arg3) {

            this.a = Utils.checkNotNull(arg2, "name == null");
            this.b = arg3;
        }

        final void apply(RequestBuilder builder, Object value) throws IOException {
            if (value == null) {
                throw new IllegalArgumentException("Method parameter \"" + this.a + "\" value must not be null.");
            }

            String v1 = this.a;
            Object v0 = this.b.convert(value);
            if (builder.httpMethod == null) {
                throw new AssertionError();
            }

            builder.httpMethod = builder.httpMethod.replace("{" + v1 + "}", ((CharSequence) v0));
        }
    }

    static final class Part extends ParameterHandler {
        private final String a;
        private final Converter b;

        Part(String arg1, Converter arg2) {

            this.a = arg1;
            this.b = arg2;
        }

        final void apply(RequestBuilder builder, Object value) {
            String v1;
            Object v0_1;
            if (value != null) {
                try {
                    v0_1 = this.b.convert(value);
                    v1 = this.a;
                } catch (IOException v0) {
                    throw new RuntimeException("Unable to convert " + value + " to RequestBody", ((Throwable) v0));
                }

                builder.multipartBody.a(v1, "binary", ((TypedOutput) v0_1));
            }
        }
    }

    static final class PartMap<T> extends ParameterHandler<Map<String, T>> {
        private final Converter<T, TypedOutput> a;
        private final String b;

        PartMap(Converter<T, TypedOutput> arg1, String arg2) {
            this.a = arg1;
            this.b = arg2;
        }

        final void apply(RequestBuilder builder, Map<String, T> arg7) throws IOException {
            if (arg7 == null) {
                throw new IllegalArgumentException("Part map was null.");
            }

            Iterator v2 = ((Map) arg7).entrySet().iterator();
            while (v2.hasNext()) {
                Object v0 = v2.next();
                Object v1 = ((Map.Entry) v0).getKey();
                if (v1 == null) {
                    throw new IllegalArgumentException("Part map contained null key.");
                }

                T value = (T) ((Map.Entry) v0).getValue();
                if (value == null) {
                    throw new IllegalArgumentException("Part map contained null value for key \'" + v1 + "\'.");
                }

                builder.multipartBody.a(((String) v1), this.b, this.a.convert(value));
            }
        }
    }

    static final class m<T> extends ParameterHandler<T> {
        private final String a;
        private final Converter<T, String> b;
        private final boolean c;

        m(String arg2, Converter<T, String> arg3, boolean arg4) {
            this.a = Utils.checkNotNull(arg2, "name == null");
            this.b = arg3;
            this.c = arg4;
        }

        final void apply(RequestBuilder builder, T value) throws IOException {
            if (value != null) {
                builder.addQueryParam(this.a, this.b.convert(value), this.c);
            }
        }
    }

    static final class QueryMap<T> extends ParameterHandler<Map<String, T>> {
        private final Converter<T, String> valueConverter;
        private final boolean encoded;

        QueryMap(Converter<T, String> arg1, boolean arg2) {
            this.valueConverter = arg1;
            this.encoded = arg2;
        }

        final void apply(RequestBuilder builder, Map<String, T> arg6) throws IOException {
            if (arg6 != null) {
                Iterator v2 = ((Map) arg6).entrySet().iterator();
                while (v2.hasNext()) {
                    Object v0 = v2.next();
                    Object v1 = ((Map.Entry) v0).getKey();
                    if (v1 == null) {
                        throw new IllegalArgumentException("Query map contained null key.");
                    } else {
                        T value = (T) ((Map.Entry) v0).getValue();
                        if (value == null) {
                            continue;
                        }
                        builder.addQueryParam(((String) v1), this.valueConverter.convert(value), this.encoded);
                        continue;
                    }
                }
            }
        }
    }
}

